$().ready(function () {
    validateRule();
    $(document).ready(function () {//通用方法去输入框去除空格
        $("form").on("change", function () {
            $("form input[type=text]").each(function () {
                this.value = this.value.replace(/\s+/g, '');
            });
        })
    });
});

$.validator.setDefaults({
    submitHandler: function () {
        update();
    }
});

function update(publishstate) {
    var articletitle = $("#articletitle").val();
    if (articletitle == null || "" == articletitle) {
        layer.alert("请输入文章标题");
        return;
    }
    var articlehistorydate = $("#articlehistorydate").val();
    if (articlehistorydate == null || "" == articlehistorydate) {
        layer.alert("请选择文章日期");
        return;
    }
    var productid = $("#productid").val();
    if (productid == null || "" == productid) {
        layer.alert("请选择产品");
        return;
    }
    var tagid = $("#tagid").val();
    if (tagid == null || "" == tagid) {
        layer.alert("请选择标签");
        return;
    }
    var v_file = $("#articlecoverurl").val();
    if (v_file == null || "" == v_file) {
        layer.alert("请上传封面图");
        return;
    }
    if (publishstate != 0) {
        var content = UE.getEditor('editor').getContent();
        if (content == null || "" == content) {
            layer.alert("请输入文章内容");
            return;
        }
        publishstate = 1;
    }
    var data = {
        id: $("#id").val(),
        articletitle: $("#articletitle").val(),
        articlehistorydate: $("#articlehistorydate").val(),
        productid: $("#productid").val(),
        tagid: $("#tagid").val(),
        articlecoverurl: $("#articlecoverurl").val(),
        publishstate: publishstate,
        articlecontent: UE.getEditor('editor').getContent()
    }
    $.ajax({
        cache: true,
        type: "POST",
        url: "/market/productArticle/update",
        data: data,// 你的formid
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }

        }
    });

}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            articletitle: {
                required: true
            },
            articlehistorydate: {
                required: true
            },
            productid: {
                required: true
            },
            tagid: {
                required: true
            },
            articlecoverurl: {
                required: true
            },
        },
        messages: {
            articletitle: {
                required: icon + "请输入文章标题"
            },
            articlehistorydate: {
                required: icon + "请选择日期"
            },
            productid: {
                required: icon + "请选择产品"
            }, tagid: {
                required: icon + "请选择标签"
            }, articlecoverurl: {
                required: icon + "请上传封面"
            },
        }
    })
}